

class PlurkUser:
  '''A user in Plurk.'''
  def __init__(self, pdict):
    '''Construct a PlurkUser from a dictionary.'''
    self.datadict = pdict
    udict = pdict['user_info'] if pdict.has_key('user_info') else pdict
    # self-identifying information
    self.uid = udict.get('uid', 0)
    self.id = udict.get('id', 0)
    self.username = udict.get('nick_name', '')
    # statistics
    self.stats = {}
    for key in ('fans_count', 'friends_count', 'alerts_count', 'unread_count'):
      self.stats[key] = pdict.get(key, '0')
    self.stats['recruited'] = udict.get('recruited', '0')
    self.stats['karma'] = udict.get('karma', '0')
    # settings that can be changed by user himself/herself
    self.settings = {}
    self.settings['privacy'] = pdict.get('privacy', '')
    self.settings['has_read_permission'] = pdict.get('has_read_permission', '')
    for key in ('display_name', 'relationship', 'has_profile_image', 'avatar', 
                'date_of_birth', 'location', 'full_name', 'gender', 'timezone'):
      self.settings[key] = udict.get(key, None)
    self.plurks = pdict.get('plurks', [])

  def __str__(self):
    result = [u'%s(%s)' % (self.username, self.id)]
    result.append(u' stats:(')
    result.append(u','.join(['%s=%s' % (k, v) for k, v in self.stats.items()]))
    result.append(u') settings:(')
    result.append(u','.join(['%s=%s' % (k, v)
                            for k, v in self.settings.items()]))
    result.append(u')')
    return u''.join(result).encode('utf8')

  def __eq__(self, other):
    return self.__class__ == other.__class__ and \
           self.uid == other.uid and self.id == other.id and \
           self.username == other.username and \
           self.settings == other.settings and \
           self.stats == other.stats
